<?php

namespace App\Models;

use EasyWeChat\Kernel\Messages\Image;
use EasyWeChat\Kernel\Messages\News;
use EasyWeChat\Kernel\Messages\NewsItem;
use Illuminate\Database\Eloquent\Model;

/**
 * Class WechatMessage
 * @package App\Models
 */
class WechatMessage extends Model
{
    const RESPONSE_TYPE_KEY = 1;                // 回复类型 - 关键字
    const RESPONSE_TYPE_RECEIVE = 2;            // 回复类型 - 收到消息回复
    const RESPONSE_TYPE_SUBSCRIBE = 3;          // 回复类型 - 被关注回复
    const MSG_TEXT = 1;                         // 回复消息类型 - 文本
    const MSG_IMAGE = 2;                        // 回复消息类型 - 图片
    const MSG_IMAGE_TEXT = 3;                   // 回复消息类型 - 图文

    public static $keyTextRules = [
        'name' => 'required',
        'type' => 'required',
        'key' => 'required',
        'message_type' => 'required',
        'title' => 'required'
    ];

    public static $keyImageRules = [
        'name' => 'required',
        'type' => 'required',
        'key' => 'required',
        'message_type' => 'required',
        'image_id' => 'required'
    ];

    public static $keyTextImageRules = [
        'name' => 'required',
        'type' => 'required',
        'key' => 'required',
        'message_type' => 'required',
        'title' => 'required',
        'description' => 'required',
        'image_url' => 'required|url',
        'url' => 'required|url'
    ];

    public static $receiveTextRules = [
        'name' => 'required',
        'type' => 'required',
        'message_type' => 'required',
        'title' => 'required'
    ];

    public static $receiveImageRules = [
        'name' => 'required',
        'type' => 'required',
        'message_type' => 'required',
        'image_id' => 'required'
    ];

    public static $receiveTextImageRules = [
        'name' => 'required',
        'type' => 'required',
        'message_type' => 'required',
        'title' => 'required',
        'description' => 'required',
        'image_url' => 'required|url',
        'url' => 'required|url'
    ];

    protected $fillable = [
        'name',
        'type',
        'key',
        'message_type',
        'title',
        'description',
        'image_id',
        'image_url',
        'url',
        'is_enable',
        'sort'
    ];

    public function scopeResponseTypeKey($query)
    {
        return $query->where('type', '=', self::RESPONSE_TYPE_KEY);
    }

    public function scopeResponseTypeReceive($query)
    {
        return $query->where('type', '=', self::RESPONSE_TYPE_RECEIVE);
    }

    public function scopeResponseTypeSubscribe($query)
    {
        return $query->where('type', '=', self::RESPONSE_TYPE_SUBSCRIBE);
    }

    public function scopeIsEnable($query)
    {
        return $query->where('is_enable', '=', 1);
    }

    /**
     * @param $query
     * @param $name
     */
    public function scopeNameLike($query, $name)
    {
        $query->when($name, function ($query) use ($name) {
            $query->where('name', 'like', '%' . $name . '%');
        });
    }

    /**
     * @return Image|News|mixed|string
     * 分别按照消息回复方式进行组装数据
     */
    public function getResponse()
    {
        switch ($this->message_type) {
            // 文本
            case WechatMessage::MSG_TEXT:
                $response = $this->title;
                break;
            // 图片
            case WechatMessage::MSG_IMAGE:
                $response = new Image($this->image_id);
                break;
            // 图文
            case WechatMessage::MSG_IMAGE_TEXT:
                $items = [
                    new NewsItem([
                        'title'         => $this->title,
                        'description'   => $this->description,
                        'url'           => $this->url,
                        'image'         => $this->image_url
                    ])
                ];
                $response = new News($items);
                break;
            default:
                $response = '';
        }
        return $response;
    }
}
